<?php
/**
 * Description of PhotoFacade
 *
 * @author TheFaSTeR
 */
class PhotoFacade
{       
    public static function getUserPhotoAlbums($userId)
    {
        return UserPhotoAlbum::model()->findAll('user_id=:userId', array(':userId' => $userId));
        //return PhotoAlbum::model()->findAll('user_id=:userId', array(':userId' => $userId));
    }
    
    public static function getCountPhotoAlbumsByUser($userId)
    {
        return UserPhotoAlbum::model()->count('user_id=:userId', array(':userId' => $userId));
    }
    
    public static function getPhotoAlbum($id)
    {
        return PhotoAlbum::model()->findByPk($id);
    }
    
    public static function getPhotoUploadsDir(PhotoAlbum $album)
    {        
        return UserFacade::getUserUploadsDir(Yii::app()->controller->getUserFromSession()) . '/albums/' . $album->album_id . '/';
    }
    
    public static function getPhotoAlbumsUploadsDir()
    {
        return UserFacade::getUserUploadsDir(Yii::app()->controller->getUserFromSession()) . '/albums';
    }
    
    public static function savePhoto($uploadImage, PhotoAlbum $album)            
    {
        $userId = Yii::app()->controller->getUserIdFromSession();
        $user = UserFacade::getUserById($userId); 
        $photo = new Photo();
        $photo->album_id = $album->album_id;
        $photo->src = $uploadImage->image_id;        
        self::saveModel($photo);
    }
    
    public static function getLatestPhotoByAlbum($albumId)
    {
        return Photo::model()->latest()->find('album_id=:albumId', array(':albumId' => $albumId));
    }
    
    public static function getAlbumLastUpdateTime($albumId)
    {
        $latestPhoto = self::getLatestPhotoByAlbum($albumId);
        return $latestPhoto->publish_date;
    }
    
    public static function getCountPhotosByAlbum($albumId)
    {
        return Photo::model()->count('album_id=:albumId', array(':albumId' => $albumId));
    }
    
    public static function getPhotosByAlbum($albumId)
    {
        return Photo::model()->findAll('album_id=:albumId', array(':albumId' => $albumId));
    }
    
    public static function getPhotoById($photoId)
    {
        return Photo::model()->findByPk($photoId);
    }
    
    public static function getAlbumByPhoto($photoId)
    {
        $photo = self::getPhotoById($photoId);
        return PhotoAlbum::model()->findByPk($photo->album_id);
    }
    
    public static function getUserByAlbum($albumId)
    {
        $useralbum = UserPhotoAlbum::model()->find('album_id=:albumId', array(':albumId' => $albumId));
        return UserFacade::getUserById($useralbum->user_id);
    }
    
    public static function deletePhoto(Photo $photo)
    {
        $imageId = $photo->src;
        $photo->delete();
        UploadImageFacade::unsetImage($imageId);
    }
    
    public static function deleteAlbum(PhotoAlbum $album)
    {
        $photos = self::getPhotosByAlbum($album->album_id);
        foreach ($photos as $photo)
        {
            self::deletePhoto($photo);
        }
        $album->delete();        
    }
    
    public static function getAllPhotos()
    {
        return Photo::model()->findAll();
    }        
    
    public static function saveModel(Photo $model)
    {
        $isNewRecord = $model->isNewRecord;
        $success = true;
        if ($isNewRecord)
        {
            $commentFlow = new CommentFlow;
            $success &= $commentFlow->save();
            $model->comment_flow_id = $commentFlow->comment_flow_id;
        }
        $success &= $model->save();
        return $success;
    }
}
